import 'package:common_utils/common_utils.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:nft_shop/box/model/models.dart';
import 'package:nft_shop/net/service_repository.dart';
import 'package:nft_shop/res/constant.dart';

class InviteBoxController extends GetxController {
  static final String InviteBoxInfo = "InviteBoxInfo";
  static final String InviteBoxChum = "InviteBoxChum";
  static final String InviteBoxOpen = "InviteBoxOpen";

  InviteInfoModel? infoModel;
  List<InviteUserList> chumList = [];
  InviteUserList? openModel;
  int residue = 0;

  ///状态
  late int status = LoadStatusType.loading;
  late int chumStatus = LoadStatusType.loading;
  late int openStatus = LoadStatusType.loading;

  static InviteBoxController get to => Get.put(InviteBoxController());

  //获取数据
  void getInviteInfo(String id) async {
    try {
      infoModel = await ServiceRepository.getInviteInfo(id: id);
      residue = infoModel!.package ?? 0;
      if (ObjectUtil.isNotEmpty(infoModel)) {
        status = LoadStatusType.success;
      } else {
        status = LoadStatusType.empty;
      }
    } catch (s, e) {
      print("执行了---");
      status = LoadStatusType.networkFailure;
    }
    update([InviteBoxInfo]);
  }

  //我的好友
  void getInviteUserList(String id, int page, refreshController) async {
    try {
      List<InviteUserList> list =
          await ServiceRepository.getInviteUserList(id: id, page: page);
      if (list.isEmpty) {
        page--;
        refreshController.loadNoData();
      } else {
        if (page == 1) {
          chumList.clear();
        }
        chumList.addAll(list);
        if (list.length < 15) {
          refreshController.loadNoData();
        } else {
          refreshController.loadComplete();
        }
      }
      if (chumList.length > 0) {
        chumStatus = LoadStatusType.success;
      } else {
        chumStatus = LoadStatusType.empty;
      }
    } catch (s, e) {
      chumStatus = LoadStatusType.networkFailure;
    }
    Future.delayed(Duration(milliseconds: 2000)).then((e) {
      update([InviteBoxChum]);
    });

  }

  //开盲盒接口
  Future openPackage(String id) async {
    try {
      openModel = await ServiceRepository.openPackage(id: id);
      if (ObjectUtil.isNotEmpty(openModel)) {
        residue -= 1;
        if (residue < 0) {
          residue = 0;
        }
        openStatus = LoadStatusType.success;
      } else {
        openStatus = LoadStatusType.empty;
      }
    } catch (s, e) {
      openStatus = LoadStatusType.networkFailure;
    }
  }
}
